﻿using Caliburn.Micro;
using KouDai.Phone.Engine;
using KouDai.Phone.Models;
using KouDai.Phone.Services;
using KouDai.Phone.Utils;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace KouDai.Phone.ViewModels
{
    public class NewRankingListViewModel : KouDaiScreen
    {
        #region Fields
        private string _startDate = "0";
        private readonly INavigationService _navigationService;
        private bool _releaseShow = false;
        private ObservableCollection<GroupCollection<HoneyEntity>> _rankingItems = new ObservableCollection<GroupCollection<HoneyEntity>>();
        #endregion

        #region Constructor
        public NewRankingListViewModel()
        {
            if (Execute.InDesignMode)
            {
                LoadDesignData();
            }
        }

        public NewRankingListViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;
        }
        #endregion

        #region Properties
      
        public ObservableCollection<GroupCollection<HoneyEntity>> RankingItems
        {
            get { return _rankingItems; }
            set
            {
                if (value != _rankingItems)
                {
                    _rankingItems = value;
                    this.NotifyOfPropertyChange(() => this.RankingItems);
                }
            }
        }

        public string CombineAppId { get; set; }

        public bool ReleaseShow
        {
            get { return _releaseShow; }
            set
            {
                if (value != _releaseShow)
                {
                    _releaseShow = value;
                    this.NotifyOfPropertyChange(() => this.ReleaseShow);
                }
            }
        }
        #endregion


        #region Override
        public override void LoadDesignData()
        {
            List<RankingListEntity> rankingList = new List<RankingListEntity>();
            rankingList.Add(new RankingListEntity()
            {
                Date = "05月17日",
                LatestUpdateTime = 1337184000000,
                UpdateTimeTip = "20:00更新",
                Week = "星期四",
                HoneyItems = new ObservableCollection<HoneyEntity>
                            {
                                new HoneyEntity(){FaviorateAmout=466,ImageUrlForIphone="http://img.geilicdn.com/taobao13813024933_146x146.jpg",ID="taobao13813024933"},
                                new HoneyEntity(){FaviorateAmout=458,ImageUrlForIphone="http://img.geilicdn.com/taobao17219460721_146x146.jpg",ID="taobao17219460721"},
                                new HoneyEntity(){FaviorateAmout=425,ImageUrlForIphone="http://img.geilicdn.com/taobao14531473910_146x146.jpg",ID="taobao14531473910"},
                                new HoneyEntity(){FaviorateAmout=421,ImageUrlForIphone="http://img.geilicdn.com/taobao17270972387_146x146.jpg",ID="taobao17270972387"},
                                new HoneyEntity(){FaviorateAmout=405,ImageUrlForIphone="http://img.geilicdn.com/taobao14678566598_146x146.jpg",ID="taobao14678566598"},
                                new HoneyEntity(){FaviorateAmout=396,ImageUrlForIphone="http://img.geilicdn.com/taobao14453269505_146x146.jpg",ID="taobao14453269505"}
                            }
            });

            foreach (RankingListEntity item in rankingList)
            {
                GroupCollection<HoneyEntity> group = new GroupCollection<HoneyEntity>(item.Date + item.UpdateTimeTip);

                foreach (var subItem in item.HoneyItems)
                {
                    group.Add(subItem);
                }
                RankingItems.Add(group);
            }
        }

        protected override void OnInitialize()
        {
            GetRankingList();
        }
        #endregion

        #region Method
        public async void GetRankingList()
        {
            BackData backData = await ServiceManager.GetRankingList(_startDate, CombineAppId);
            if (backData.IsSuccess)
            {
                List<RankingListEntity> rankingListItems = backData.ResposeData as List<RankingListEntity>;
                this.RankingItems = TransferLongListCollection(rankingListItems);
                _startDate = rankingListItems[0].LatestUpdateTime.ToString();
            }
            else
            {
                Debug.WriteLine(backData.Msg);
            }
        }

        private ObservableCollection<GroupCollection<HoneyEntity>> TransferLongListCollection(List<RankingListEntity> rankListitems)
        {
            ObservableCollection<GroupCollection<HoneyEntity>> transferItems = new ObservableCollection<GroupCollection<HoneyEntity>>();
            foreach (RankingListEntity item in rankListitems)
            {
                GroupCollection<HoneyEntity> group = new GroupCollection<HoneyEntity>(item.Date + item.UpdateTimeTip);
                foreach (var subItem in item.HoneyItems)
                {
                    group.Add(subItem);
                }
                transferItems.Add(group);
            }
            return transferItems;
        }
        #endregion

        #region ViewMethod
        public void RankSelectExecute(HoneyEntity selectedItem)
        {
            Debug.WriteLine("url" + selectedItem.ImageUrlForIphone.ToString());
        }

        public void RealizeItemExecute(ItemRealizationEventArgs e)
        {
            if (e.ItemKind == LongListSelectorItemKind.Item)
            {
                HoneyEntity honeyItem = e.Container.Content as HoneyEntity;
                GroupCollection<HoneyEntity> rankItem = _rankingItems.Where(x => x.Contains(honeyItem)).FirstOrDefault();
                if (_rankingItems.IndexOf(rankItem) == 0)
                {
                    Debug.WriteLine("daotoule -------------> ");
                }
            }
        }

        public void PullDownRefresh()
        {
            Debug.WriteLine("刷新---->");
        }
        #endregion
    }
}
